package com.example.demo.serviceimpl;

import com.example.demo.po.User;
import com.example.demo.repositroy.UserDao;
import com.example.demo.service.UserService;
import com.example.demo.utils.Singel2Utils;
import com.example.demo.utils.SingelUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void save(User user) {
        SingelUtils singelUtils = SingelUtils.getSingelUtils();
        Singel2Utils singel2Utils = Singel2Utils.getSingel2Utils();
        userDao.save(user);
    }

    @Override
    public void updateUserInfo(User user) {
        User userOld = userDao.findById(user.getId()).orElse(null);
        log.info("userOld={}",userOld);
        if(userOld == null){
            throw new BuilderException("未查到相关信息！");
        }
        userOld.copy(user);
        userDao.save(userOld);
    }

    @Override
    public User findUserByUserNumber(String userNumber) {
        return userDao.findUserByUserNumber(userNumber);
    }
}
